home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / nktools.zip / STRDEV.PAS < prev    next >
Pascal/Delphi Source File  |  1991-02-11  |  8KB  |  199 lines

  1. unit StrDev;
  2. (*===================================================================*\
  3. || UNIT NAME:    StrDev                                              ||
  4. || DEPENDENCIES: Dos.TPU                                             ||
  5. || PROGRAMMER:   Naoto Kimura                                        ||
  6. || LAST MOD ON:  9102.11                                             ||
  7. ||                                                                   ||
  8. || DESCRIPTION:  This is a text file device driver for printing to a ||
  9. ||               string.  The control for a text file is re-routed   ||
  10. ||               to send output to a string buffer instead of a file ||
  11. ||               or device.                                          ||
  12. \*===================================================================*)
  13. interface
  14.  
  15. uses dos;
  16.  
  17. var
  18.     StrDevice : Text;
  19.  
  20. (*-------------------------------------------------------------------*\
  21. | NAME: AssignStr                                                     |
  22. |                                                                     |
  23. |     This routine is used to associate a text file variable with a   |
  24. | string output buffer.                                               |
  25. \*-------------------------------------------------------------------*)
  26. procedure AssignStr( var F    : Text );
  27.  
  28. (*-------------------------------------------------------------------*\
  29. | NAME: GetStrBuf                                                     |
  30. |                                                                     |
  31. |      This routine returns the accumilated string output and clears  |
  32. | the buffer.                                                         |
  33. \*-------------------------------------------------------------------*)
  34. function GetStrBuf( var F    : Text ) : String;
  35.  
  36. implementation
  37.  
  38. (*-------------------------------------------------------------------*\
  39. | This record type defines the structure of the data stored in a file |
  40. | variable type in the UserData field.  It contains information for   |
  41. | the string buffer to which output is sent.                          |
  42. \*-------------------------------------------------------------------*)
  43. type
  44.     StrDevRec    = record
  45.         case Boolean of
  46.         False:    ( Unused    : array [0..15] of byte );
  47.         True:    ( StrBuf    : ^String )
  48.         end;
  49.  
  50. {$F+}    (* force FAR reference *)
  51.  
  52. (*-------------------------------------------------------------------*\
  53. | NAME: GetStrBuf                                                     |
  54. |                                                                     |
  55. |      This routine returns the accumilated string output and clears  |
  56. | the buffer.                                                         |
  57. \*-------------------------------------------------------------------*)
  58. function GetStrBuf( var F    : Text ) : String;
  59.     begin
  60.     GetStrBuf := StrDevRec(TextRec(F).UserData).StrBuf^;
  61.     StrDevRec(TextRec(F).UserData).StrBuf^ := ''
  62.     end;    (* GetStrBuf *)
  63.  
  64. (*-------------------------------------------------------------------*\
  65. | NAME:  StrOutput                                                    |
  66. |                                                                     |
  67. |     This is the output handling routine for files assigned to the   |
  68. | string output device.   This is an internal service routine and     |
  69. | will not be directly used by any procedure outside of this unit.    |
  70. |                                                                     |
  71. | EXTERNALS: type      TextRec (Dos), StrDevRec                       |
  72. \*-------------------------------------------------------------------*)
  73. {static far} function StrOutput(var f : TextRec) : integer;
  74.     var
  75.     p    : word;
  76.     begin
  77.     with f,StrDevRec(UserData) do begin
  78.         p := 0;
  79.         while p < BufPos do begin
  80.         StrBuf^ := StrBuf^ + BufPtr^[p];
  81.         Inc(p)
  82.           end;
  83.         BufPos := 0
  84.       end;
  85.     StrOutput := 0
  86.     end;   (* StrOutput *)
  87.  
  88. (*-------------------------------------------------------------------*\
  89. | NAME:  StrIgnore                                                    |
  90. |                                                                     |
  91. | This routine is used to perform a do-nothing function, usually for  |
  92. | don't care conditions that may occur during I/O.  This is an        |
  93. | internal service routine and will not be directly used by any       |
  94. | procedure outside of this unit.                                     |
  95. |                                                                     |
  96. | EXTERNALS: type TextRec (Dos)                                       |
  97. \*-------------------------------------------------------------------*)
  98. {static far} function StrIgnore(var f : TextRec) : integer;
  99.     begin
  100.     StrIgnore := 0
  101.     end;   (* StrIgnore *)
  102.  
  103. (*-------------------------------------------------------------------*\
  104. | NAME:  StrClose                                                     |
  105. |                                                                     |
  106. | This routine is used to close an output stream to a string device.  |
  107. | It is assumed that an AssignStr has been performed on the text file |
  108. | variable to open it, and then Rewrite to actually open it.  This is |
  109. | an internal service routine and will not be directly used by any    |
  110. | procedure outside of this unit.                                     |
  111. |                                                                     |
  112. | EXTERNALS: type      TextRec (Dos)                                  |
  113. \*-------------------------------------------------------------------*)
  114. {static far} function StrClose(var f : TextRec) : integer;
  115.     begin
  116.     with f,StrDevRec(UserData) do begin
  117.         Dispose(StrBuf)
  118.       end;
  119.     StrClose := 0
  120.     end;   (* StrClose *)
  121.  
  122. (*-------------------------------------------------------------------*\
  123. | NAME:  StrOpen                                                      |
  124. |                                                                     |
  125. | This routine is used to open an output stream to a string device.   |
  126. | It is assumed that an AssignStr has been performed on the text file |
  127. | variable.  This is an internal service routine and will not be      |
  128. | directly used by any procedure outside of this unit.                |
  129. |                                                                     |
  130. | EXTERNALS: type      TextRec (Dos)                                  |
  131. |            function  StrInput, StrOutput, StrIgnore                 |
  132. \*-------------------------------------------------------------------*)
  133. {static far} function StrOpen(var f : TextRec) : integer;
  134.     const
  135.     ErrMsg    : string
  136.     = #13#10'StrDev unit: string device is write-only !'#13#10'$';
  137.     var
  138.     regs    : Registers;
  139.     begin
  140.     with f,StrDevRec(UserData) do begin
  141.         BufPos := 0;
  142.         BufEnd := 0;
  143.  
  144.         If Mode=fmInput then begin
  145.         Regs.DS := Seg(ErrMsg[1]);
  146.         Regs.DX := Ofs(ErrMsg[1]);
  147.         Regs.AH := $09;
  148.         Intr($21,Regs);
  149.         Halt
  150.           end
  151.         else begin
  152.         New(StrBuf);
  153.         StrBuf^   := '';
  154.         Mode      := fmOutput;
  155.         InOutFunc := @StrOutput;
  156.         FlushFunc := @StrOutput
  157.           end;
  158.         CloseFunc := @StrClose
  159.       end;
  160.     StrOpen := 0
  161.     end;   (* StrOpen *)
  162.  
  163. (*-------------------------------------------------------------------*\
  164. | NAME: AssignStrDev                                                  |
  165. |                                                                     |
  166. |      This routine returns the accumilated string output and clears  |
  167. | the buffer.                                                         |
  168. |                                                                     |
  169. | EXTERNALS: const     fmClosed                                       |
  170. |            function  StrOpen                                        |
  171. \*-------------------------------------------------------------------*)
  172. procedure AssignStr( var F    : Text );
  173.     begin
  174.     with TextRec(f) do begin
  175.         Handle    := $FFFF;
  176.         Mode    := fmClosed;
  177.         BufSize    := sizeof(Buffer);
  178.         BufPtr    := @Buffer;
  179.         OpenFunc    := @StrOpen;
  180.         Name[0] := #0
  181.       end
  182.     end;    (* AssignStr *)
  183.  
  184. var
  185.     OldExitProc    : Pointer;
  186.  
  187. {static far} procedure Cleanup;
  188.     begin
  189.     ExitProc := OldExitProc;
  190.     Close(StrDevice)
  191.     end;    (* Cleanup *)
  192.  
  193. begin
  194.     AssignStr( StrDevice );
  195.     Rewrite(StrDevice);
  196.     OldExitProc := ExitProc;
  197.     ExitProc := @Cleanup
  198. end.
  199.